use crate::player::player::{PlayerExp, PlayerLevel, PlayerUpExp, Scoreboard, INITIAL_UP_MXP_LEVEL_EXP};
use bevy::prelude::*;

///游戏分数ui
#[derive(Component)]
pub struct ScoreboardUi;

#[derive(Component)]
pub struct LevelUi;

#[derive(Component)]
pub struct ExpBar;

#[derive(Component)]
pub struct ExpText;

const SCOREBOARD_TEXT_PADDING: Val = Val::Px(10.0);

pub fn spawn_scoreboard_ui(mut com: Commands, asset_server: Res<AssetServer>) {
    com.spawn((
        ScoreboardUi,
        TextBundle::from_sections([
            TextSection::new(
                "Score: ",
                TextStyle {
                    font: asset_server.load("ttf/fusion-pixel.ttf"),
                    font_size: 30.,
                    color: Color::RED,
                    ..default()
                },
            ),
            TextSection::from_style(TextStyle {
                font: asset_server.load("ttf/fusion-pixel.ttf"),
                font_size: 30.,
                color: Color::RED,
                ..default()
            }),
        ])
        .with_style(Style {
            position_type: PositionType::Absolute,
            top: SCOREBOARD_TEXT_PADDING,
            left: SCOREBOARD_TEXT_PADDING,
            ..default()
        }),
    ));
}

pub fn spawn_level_ui(mut com: Commands, asset_server: Res<AssetServer>) {
    com.spawn(NodeBundle {
        style: Style {
            position_type: PositionType::Absolute,
            top: Val::Px(0.),
            width: Val::Percent(60.),
            height: Val::Px(50.),
            justify_self: JustifySelf::Center,
            ..default()
        },
        // background_color: Color::PINK.into(),
        ..Default::default()
    })
    .with_children(|parent| {
        parent.spawn((
            LevelUi,
            TextBundle::from_sections([
                TextSection::new(
                    "Level: ",
                    TextStyle {
                        font: asset_server.load("ttf/fusion-pixel.ttf"),
                        font_size: 24.,
                        color: Color::RED,
                        ..default()
                    },
                ),
                TextSection::from_style(TextStyle {
                    font: asset_server.load("ttf/fusion-pixel.ttf"),
                    font_size: 24.,
                    color: Color::RED,
                    ..default()
                }),
            ])
            .with_style(Style {
                position_type: PositionType::Absolute,
                left: Val::Px(0.),
                align_self: AlignSelf::Center,
                ..default()
            }),
        ));
        //经验条背景
        parent
            .spawn(NodeBundle {
                style: Style {
                    position_type: PositionType::Absolute,
                    left: Val::Px(120.),
                    right: Val::Px(0.),
                    height: Val::Px(30.),
                    align_self: AlignSelf::Center,
                    ..default()
                },
                background_color: Color::WHITE.into(),
                ..Default::default()
            })
            .with_children(|parent| {
                //经验条约束框
                parent
                    .spawn(NodeBundle {
                        style: Style {
                            position_type: PositionType::Absolute,
                            left: Val::Px(5.),
                            right: Val::Px(5.),
                            top: Val::Px(5.),
                            bottom: Val::Px(5.),
                            ..default()
                        },
                        ..Default::default()
                    })
                    .with_children(|parent| {
                        //经验条
                        parent.spawn((
                            ExpBar,
                            NodeBundle {
                                style: Style {
                                    position_type: PositionType::Absolute,
                                    left: Val::Px(0.),
                                    right: Val::Percent(0.),
                                    top: Val::Px(0.),
                                    bottom: Val::Px(0.),
                                    ..default()
                                },
                                background_color: Color::BLUE.into(),
                                ..Default::default()
                            },
                        ));
                    });
                //经验text
                parent.spawn((
                    ExpText,
                    TextBundle::from_sections([
                        TextSection::from_style(TextStyle {
                            font: asset_server.load("ttf/fusion-pixel.ttf"),
                            font_size: 24.,
                            color: Color::RED,
                            ..default()
                        }),
                        TextSection::new(
                            format!("/{}", INITIAL_UP_MXP_LEVEL_EXP),
                            TextStyle {
                                font: asset_server.load("ttf/fusion-pixel.ttf"),
                                font_size: 24.,
                                color: Color::RED,
                                ..default()
                            },
                        ),
                    ])
                    .with_style(Style {
                        position_type: PositionType::Absolute,
                        left: Val::Percent(50.),
                        ..default()
                    }),
                ));
            });
    });
}

pub fn update_scoreboard(
    scoreboard: Res<Scoreboard>,
    mut query: Query<&mut Text, With<ScoreboardUi>>,
) {
    let mut text = query.single_mut();
    text.sections[1].value = scoreboard.score.to_string();
}

pub fn update_player_bar(
    mut query_level_ui: Query<&mut Text, With<LevelUi>>,
    mut query_exp_text: Query<&mut Text, (With<ExpText>, Without<LevelUi>)>,
    mut query_bar_style: Query<&mut Style, With<ExpBar>>,
    palyer_level: ResMut<PlayerLevel>,
    palyer_exp: ResMut<PlayerExp>,
    paleyr_up_exp: ResMut<PlayerUpExp>,
) {
    let mut text_level = query_level_ui.single_mut();
    let mut exp_level = query_exp_text.single_mut();
    text_level.sections[1].value = palyer_level.level.to_string();
    exp_level.sections[0].value = palyer_exp.exp.to_string();
    exp_level.sections[1].value = format!("/{}", paleyr_up_exp.exp);
    for mut style in query_bar_style.iter_mut() {
        style.right = Val::Percent(
            (100. - (((palyer_exp.exp as f32) / (paleyr_up_exp.exp as f32)) * 100.))
                .clamp(0., 100.),
        );
    }
}
